using UnityEngine;

[ExecuteInEditMode, RequireComponent(typeof(UIRoot))]
public class UIRootScale : MonoBehaviour
{
    public int aspectHeight = 720;
    public int aspectWidth = 1280;

	public bool runOnlyOnce;
	private bool mStarted;

    private UIRoot mRoot;
    private void Awake()
	{
        mRoot = GetComponent<UIRoot>();
        UICamera.onScreenResize += ScreenSizeChanged;
    }

	private void OnDestroy()
	{
        UICamera.onScreenResize -= ScreenSizeChanged;
    }

	private void Start()
	{
        Update();
        mStarted = true;
    }
    
	private void Update()
	{
		float num = (float)aspectWidth / aspectHeight;
		float num2 = (float)Screen.width / Screen.height;

		if (num > num2)
		{
			int manualHeight = Mathf.FloorToInt(aspectWidth / num2);
			mRoot.manualHeight = manualHeight;
		}
		else
		{
			mRoot.manualHeight = aspectHeight;
		}
		if (runOnlyOnce && Application.isPlaying)
		{
			enabled = false;
		}
        ZEventCenter.DispatchEvent(new ZEvent(ConstData.SCREEN_SIZE_CHANGED));
	}

    private void ScreenSizeChanged()
    {
        if (mStarted && runOnlyOnce)
        {
            Update();
        }
    }
}